package com.cdu.guoxinan.www.smartcom.service.impl;

import com.cdu.guoxinan.www.smartcom.domian.dto.PaymentDTO;
import com.cdu.guoxinan.www.smartcom.domian.dto.UserFeeQueryDTO;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Fee;
import com.cdu.guoxinan.www.smartcom.domian.vo.FeeStatisticsVO;
import com.cdu.guoxinan.www.smartcom.mapper.UserFeeMapper;
import com.cdu.guoxinan.www.smartcom.service.UserFeeService;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserFeeServiceImpl implements UserFeeService {
    
    @Resource
    private UserFeeMapper userFeeMapper;
    
    @Override
    public R queryUserFees(UserFeeQueryDTO queryDTO) {
        // 设置分页信息
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getLimit());
        List<Fee> list = userFeeMapper.queryUserFees(queryDTO);
        PageInfo pageInfo = new PageInfo(list);
        return R.ok(pageInfo.getList(), pageInfo.getTotal());
    }
    
    @Override
    public FeeStatisticsVO getUserFeeStatistics(Long userId) {
        return userFeeMapper.getUserFeeStatistics(userId);
    }
    
    @Override
    @Transactional
    public R payFee(PaymentDTO paymentDTO) {
        // 查询费用详情
        Fee fee = userFeeMapper.getFeeById(paymentDTO.getFeeId());
        if (fee == null) {
            return R.error("费用不存在");
        }
        
        // 检查费用状态
        if (fee.getStatus() == 1) {
            return R.error("该费用已缴纳");
        }
        
        // 更新费用状态为已缴
        int result = userFeeMapper.updateFeeAsPaid(paymentDTO.getFeeId(), paymentDTO.getPaymentMethod());
        if (result > 0) {
            return R.ok("缴费成功");
        } else {
            return R.error("缴费失败");
        }
    }
    
    @Override
    public Fee getFeeById(Long feeId) {
        return userFeeMapper.getFeeById(feeId);
    }
} 